/*
 * @Author: your name
 * @Date: 2022-01-21 22:16:17
 * @LastEditTime: 2022-01-21 22:18:10
 * @LastEditors: your name
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /goose/src/middlewares/request-context.middleware.ts
 */
import { RequestContext } from '../context/request.context';
import { getNamespace, createNamespace } from 'node-request-context';
import { Request, Response } from 'express';

/**
 * 请求上下文中间件
 * @param req 请求参数
 * @param res 返回参数
 * @param next next函数
 */
export function RequestContextMiddleware(req: Request, res: Response, next: any) {
  const requestContext = new RequestContext(req, res);
  const namespace = getNamespace('lantsang.iot') || createNamespace('lantsang.iot');
  namespace.run(() => {
    namespace.set('tid', requestContext);
    next();
  });
}
